const dateFormat = (date, fmt)  => { 
    var o = {
      'y+': date.getFullYear(),//年份
      'M+': date.getMonth() + 1, // 月份
      'd+': date.getDate(), // 日
      'H+': date.getHours(), // 小时
      'm+': date.getMinutes(), // 分
      's+': date.getSeconds(), // 秒
      'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
      S: date.getMilliseconds() // 毫秒
    }
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    for (var k in o) { if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) }
    return fmt
}

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatMinDay = date => {
  const hour = date.getHours()
  const minute = date.getMinutes()
  return [hour, minute].map(formatNumber).join(':')
}

const formatTimeNoSecond = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}

const formatChineseDate = date => {
  let year = date.getFullYear();
  let month = formatMonth(date.getMonth());
  let day = date.getDate();
  return [year, month, day].map(formatNumber).join('-')
}

const formatNormalDate = date => {
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  return [year, month, day].map(formatNumber).join('-')
}

/** 
 * @description 格式化月份，让前端看起来更好看 
 */
function formatMonth(month) {
  var cn = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
  return cn[month];
}

/**
 * @description 时间戳相减获取天数相差多少
 */
function subtractTimeStampToDay(t1, t2) {
  return parseInt((t2 - t1) / (1000 * 24 * 60 * 60));
}


const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * @author hhk
 * @date 2021-07-22
 * @description 字符串判空
 */
const isNullOrEmpty = str => {
  return typeof (str) == "undefined" || str == null || str == "";
}

/**
 * @author hhk
 * @date 2021-08-09
 * @description 对象判空
 */
const objIsNull = obj => {
  return !obj || JSON.stringify(obj) == "{}";
}

/**
 * 判断参数是否为对象
 * 
 * @author hhk
 * @date 2021-10-25
 */
function isObj(obj) {
  return 'object' === typeof obj && null !== obj && !Array.isArray(obj);
}

/**
 * @author hhk
 * @date 2021-08-09
 * @description 表单验证是否填写完整
 */
const formValidation = form => {
  for (let item in form) {
    if (!form[item] || form[item] == '') {
      return false;
    }
  }
  return true;
}

/**
 * 对象数组内根据某个参数排序
 * @params props 对象参数的名字
 * @author hhk
 * @date 2021-08-30
 */
function sortBy(props) {
  return function (a, b) {
    return a[props] - b[props]; // 如果a>=b，返回自然数，不用交换位置
  }
}

/**
 * 对象数组内根据时间参数排序
 * 注意：a[props]和b[props]不可以为空或undefinded，否则会报 Cannot read property ‘replace‘ of undefined
 * @params sign 要替换的符号
 * @params props 对象参数名字
 * @author hhk
 * @date 2021-08-30
 */
function dateTimeSortBy(sign, props) {
  return function (a, b) {
    let str = new RegExp(sign, 'g');
    // 把时间参数的格式转换成 YYYY-MM-DD hh:mm:ss
    // 然后再parse
    let t1 = new Date(Date.parse(a[props].replace(str, "/")));
    let t2 = new Date(Date.parse(b[props].replace(str, "/")));
    return t2.getTime() - t1.getTime();
  }
}

/**
 * 隐藏转发按钮
 * 
 * @author hhk
 * @date 2021-10-25
 */
function hideShareMenu() {
  wx.hideShareMenu({
    menus: ['shareAppMessage', 'shareTimeline']
  })
}

/**
 * 显示加载中提示
 * tips：记得在onReady()中调用hideLoading()
 * @author hhk
 * @date 2021-10-25
 */
function showLoading() {
  wx.showLoading({
    title: '加载中...',
  })
}

/**
 * 显示提示窗口
 * @param {*} str 显示的文本
 * @param {*} icon 图标，success fail none
 * 
 * @author hhk
 * @date 2021-10-25
 */
function showToast(str, icon) {
  wx.showToast({
    title: str,
    icon: icon == 'success' ? icon : icon == 'fail' ? icon : 'none'
  })
}

/**
 * 上个月的第一天
 */
function preMonthFirstDay(nowdays) {
    var year = nowdays.getFullYear();  
    var month = nowdays.getMonth();  
    if(month == 0) {  
        month=12;  
        year=year-1;  
    }  
    if (month < 10) {  
        month = "0" + month;  
    }  
    return year + "-" + month + "-" + "01";
}
/**
 * 上个月的最后一天
 */
function preMonthLastDay(nowdays) {
    var year = nowdays.getFullYear();  
    var month = nowdays.getMonth();  
    if(month == 0) {  
        month=12;  
        year=year-1;  
    }  
    if (month < 10) {  
        month = "0" + month;  
    }  
    var myDate = new Date(year, month, 0);
    return year + "-" + month + "-" + myDate.getDate();
}

module.exports = {
  dateFormat: dateFormat,
  formatTime: formatTime,
  preMonthFirstDay: preMonthFirstDay,
  preMonthLastDay: preMonthLastDay,
  formatChineseDate: formatChineseDate,
  formatNormalDate: formatNormalDate,
  formatMinDay: formatMinDay,
  formatTimeNoSecond: formatTimeNoSecond,
  subtractTimeStampToDay: subtractTimeStampToDay,
  isNullOrEmpty: isNullOrEmpty,
  objIsNull: objIsNull,
  isObj: isObj,
  formValidation: formValidation,
  sortBy: sortBy,
  dateTimeSortBy: dateTimeSortBy,
  hideShareMenu: hideShareMenu,
  showLoading: showLoading,
  showToast: showToast
}